<template>
  <div>
<!--    <div class="table-operator" v-if="stage !== '05010' && stage !== '05050'">
      <a-button :disabled="!!status &&  status != '0' && status != '1'"
                v-permission="['10030']"
                type="primary"
                @click="showConfirm">
        提交审核
      </a-button>
      <a-button :disabled="status !== '2'"
                v-permission="['10040']"
                @click="setApproveReason('9')">
        同意
      </a-button>
      <a-button :disabled="status !== '2'"
                v-permission="['10040']"
                type="danger"
                @click="setApproveReason('1')">
        拒绝
      </a-button>
      <a-button @click="showShjl" style="margin-top:10px">审核记录</a-button>
    </div>-->
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      class="table_header_blue"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange">
      <span slot="actionName">
        <a-space v-if="stage !== '05010' && stage !== '05050' && (!status ||  status == '0' || status == '1') " v-permission="['10030']">
<!--          <a-button size="small" @click="handleAdd('创建方案')" type="primary" icon="plus">
            创建方案
          </a-button>-->
          <a-button size="small" @click="downLoadTemplete()" type="primary" icon="download">
            选择模板
          </a-button>
          <a-button size="small" @click="uploadFa()" type="primary" icon="upload">
            上传方案
          </a-button>
        </a-space>
         <span v-else>操作</span>
      </span>
      <span slot="action" slot-scope="text, record" v-if="stage !== '05010' && stage !== '05050' && (!status ||  status == '0' || status == '1')">
        <a @click="handleEdit(record,'编辑方案')">编辑</a>
        <a-divider type="vertical"/>
        <a @click="OnDownLoad(record)">下载</a>
        <a-divider type="vertical"/>
        <a @click="OnPerview(record)">预览</a>
        <a-divider type="vertical"/>
        <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
<!--        <a-dropdown>
          <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="OnDownLoad(record)">下载</a>
            </a-menu-item>
              <a-menu-item>
              <a @click="OnPerview(record)">预览</a>
            </a-menu-item>
            <a-menu-item>
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown>-->
      </span>
      <span slot="action" slot-scope="text, record" v-else>
        <a-button type="link" @click="OnPerview(record)">预览</a-button>
        <a-divider type="vertical"/>
        <a-button type="link" @click="OnDownLoad(record)">下载</a-button>
      </span>
    </a-table>
    <contentView ref="modalForm" :id="id" @ok="modalFormOk"></contentView>
    <SelectApprove ref='approver' :id='id' @submitData='handleSubmit'></SelectApprove>
    <SetReason ref='setReason' @approveReason='approveData'></SetReason>
    <shjlModal ref="shjlModal" :id='id' :typeId='typeId' :apiUrl='url.applyLog'></shjlModal>
    <ModelForm ref="uploadModel"
               :id="id"
               :stage="stage"
               :type="proType"
               :type-name="proTypeName"
               @ok="uploadData" />
    <TempleteDownload ref="templeteDownload" :type="proType" />
  </div>
</template>

<script>
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import {butStatus} from '../public/butStatus'
  import columns from './indexColumns'
  import contentView from './contentView'
  import SelectApprove from '../public/SelectApprover'
  import SetReason from "../public/SetReason"
  import shjlModal from "../shjlModal"
  import { Base64 } from 'js-base64'
  import { getAction } from '@/api/manage'
  import ModelForm from '@views/sj/sjxmzygl/commView/ssfa/ModelForm.vue'
  import TempleteDownload from '@views/sj/sjxmzygl/commView/ssfa/templeteDownload.vue'

  export default {
    props: ['id', 'type', 'stage', 'proType', 'proTypeName'],
    name: "ssfa",
    components: {
      TempleteDownload,
      ModelForm,
      contentView,
      shjlModal,
      SetReason,
      SelectApprove
    },
    mixins: [JeecgListMixin, butStatus],
    data() {
      return {
        queryParam: {
          proid: this.id,
          stage: this.stage,
          typeId: this.type
        },
        typeId: this.type,
        columns: columns(this),
        url: {
          list: '/sjxmzygl/sjWorkZtfa/list',
          delete: '/sjxmzygl/sjWorkZtfa/delete',
          submit: '/sjxmzygl/sjWorkUsers/submitAudit',
          approve: '/sjxmzygl/sjWorkUsers/zyApplyCommit',
          applyLog: '/sjxmzygl/sjWorkApplyLog/queryApplyLog',
          generateFaFile: '/sjxmzygl/sjWorkZtfa/generateFaFile',
          queryWordTemplateByType: '/sjxmzygl/sjWorkZtfa/queryWordTemplateByType',
        },
      }
    },
    methods: {
      OnDownLoad(data) {
        this.$message.success('正在下载，请稍等！')
        if(data.fileId){
          let url = `${window._CONFIG['domianURL']}/sys/common/static${data.fileUrl}`
          window.open(url, '_blank')
        }else{
          getAction(this.url.generateFaFile, {proId: data.proId}).then((res)=>{
            if(res.code == 0) {
              let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
              window.open(url, '_blank')
            }
          })
        }
      },
      OnPerview(data) {
        if(data.fileId){
          let fileUrl = `${window._CONFIG['onlinePreviewFileURL']}/sys/common/static${data.fileUrl}`
          // let url = `${window._CONFIG['onlinePreviewDomainURL']}?url=${encodeURIComponent(fileUrl)}`
          let url = `${window._CONFIG['onlinePreviewDomainURL']}?url=${encodeURIComponent(Base64.encode(fileUrl))}`
          window.open(url, '_blank')
        }else{
          getAction(this.url.generateFaFile, {proId: data.proId}).then((res)=>{
            if(res.code == 0) {
              let fileUrl = `${window._CONFIG['onlinePreviewFileURL']}/sys/common/static${res.result}`
              // let url = `${window._CONFIG['onlinePreviewDomainURL']}?url=${encodeURIComponent(fileUrl)}`
              let url = `${window._CONFIG['onlinePreviewDomainURL']}?url=${encodeURIComponent(Base64.encode(fileUrl))}`
              window.open(url, '_blank')
            }
          })
        }
      },

      handleEdit: function (record, name) {
        if(record.fileId){
          this.$refs.uploadModel.title = name
          this.$refs.uploadModel.disableSubmit = false
          this.$refs.uploadModel.edit(record)
        }else{
          this.$refs.modalForm.edit(record)
          this.$refs.modalForm.title = name
          this.$refs.modalForm.isCheck = false
          this.$refs.modalForm.disableSubmit = false
        }
      },

      modalFormOk() {
        this.loadData();
      },

      downLoadTemplete() {
        this.$refs.templeteDownload.disableSubmit = false
        this.$refs.templeteDownload.init()
      },

      uploadFa() {
        this.$refs.uploadModel.disableSubmit = false
        this.$refs.uploadModel.add()
      },

      uploadData() {
        this.loadData()
      }
    }
  }
</script>

<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';
</style>